﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Aj.Core;

public partial class Manager_SysModuleEdit : AuthPageBase
{
    protected bool isUpdate;
    protected string id = "";
    protected string pid = "0";
    protected string mname = "";
    protected string href = "#";
    protected DataSet ds = null;
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        try
        {
            id = QueryParam("id");
            pid = QueryParam("pid", "0");
            if (!string.IsNullOrEmpty(id))
            {
                isUpdate = true;
                ds = CustomQuery.SysModule_GetModuleById(id);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    mname = ds.Tables[0].Rows[0]["mname"].ToString();
                    href = ds.Tables[0].Rows[0]["href"].ToString();
                    pid = ds.Tables[0].Rows[0]["parentid"].ToString();
                }
            }
            else
            {
                if (IsGet)
                {
                    object obj = CustomQuery.SysModule_GetMaxMidByPid(pid);
                    id = DBNull.Value != obj && null != obj ? obj.ToString() : "";
                    if (id.Length == 2)
                    {
                        int _id = id.ToInt() + 1;
                        id = (_id < 10 ? "0" : "") + (_id).ToString();
                    }
                    else if (id.Length == 4)
                    {
                        int _id = id.ToInt() + 1;
                        id = (_id < 1000 ? "0" : "") + (_id).ToString();
                    }
                    else
                    {
                        id = (pid.IsNotNullAndEmpty() ? pid : "") + "01";
                    }
                }
            }

            if (IsPost)
            {
                // 更新
                if (!string.IsNullOrEmpty(id))
                {
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        int rel = CustomQuery.SysModule_Update(id, FormParam("pid"), FormParam("mname"), FormParam("mname"), FormParam("href"));
                        if (rel > 0)
                        {
                            Page_GoTo("SysModules.aspx", true);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                        }
                    }
                }
                else // 新建
                {
                    // 先检查一下
                    ds = CustomQuery.SysModule_GetModuleById(FormParam("mid"));
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox("该id[" + FormParam("mid") + "]已经存在了！");
                    }
                    else
                    {
                        object obj = CustomQuery.SysModule_GetModuleSort(pid);
                        int sort = DBNull.Value != obj && null != obj ? Convert.ToInt32(obj) : 0;
                        int rel = CustomQuery.SysModule_Add(FormParam("mid"), pid, FormParam("mname"), FormParam("mname"), FormParam("href"), sort);
                        if (rel > 0)
                        {
                            Page_GoTo("SysModules.aspx", true);
                        }
                        else
                        {
                            MessageBox("新建失败！");
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
        }
    }
}